package com.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.constant.RedisConstant;
import com.entity.PageResult;
import com.entity.QueryPageBean;
import com.entity.Result;
import com.pojo.Setmeal;
import com.service.SetMealService;
import com.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;
    @Autowired  //使用JedisPool来控制redis
    private JedisPool jedisPool;

    @RequestMapping("/upload")   //图片上传
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();//获取原始路径名
        int index = originalFilename.lastIndexOf(".");//获取原路径吗最后一个点的索引位置
        String substring = originalFilename.substring(index);//截取最后一个点索引往后的数据
        String imageName = UUID.randomUUID().toString() + substring;//随机生成一个32位的字符串
        try {
            //调用七里云工具类上传文件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),imageName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imageName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,imageName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
//        return null;
    }

    @RequestMapping("/add")     //添加套餐数据
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setMealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")   //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setMealService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById")    //通过id查询检查套餐数据
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findCheckGroupBySetMeal") //通过套餐id查询其包含的检查组
    public Result findCheckGroupBySetMeal(Integer id){
        try {
            List<Integer> list = setMealService.findCheckGroupBySetMeal(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")    //数据修改
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setMealService.edit(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑检查套餐失败");
        }
        return new Result(true,"编辑检查套餐成功");
    }

    @RequestMapping("delete")  //删除套餐
    public Result delete(@RequestBody Setmeal setmeal){
        try {
            setMealService.delete(setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
        return new Result(true,"删除套餐成功");
    }
}
